package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.PageInf;
import com.cykj.v_m_sprintboot.bean.TbWarehouse;
import com.cykj.v_m_sprintboot.service.TbWarehouseService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AddWarehouseController {
    @Autowired
    private TbWarehouseService tbWarehouseService;

    //添加仓库的方法
    @RequestMapping("/addWarehouse")
    @ResponseBody
    public String addWarehouse(@RequestBody TbWarehouse tbWarehouse) {
        if (tbWarehouse != null) {
            System.out.println(tbWarehouse.getWarehouseName() + "," + tbWarehouse.getWarehouseSite());
            tbWarehouse.setWarehouseStateId(1);
            TbWarehouse tw = tbWarehouseService.judgeWarehouse(tbWarehouse);
            if (tw == null) {
                TbWarehouse th = new TbWarehouse(tbWarehouse.getWarehouseName(), tbWarehouse.getWarehouseSite(), GetTime.getTime(), GetTime.getTime());
                boolean flag = tbWarehouseService.addWarehouse(th);
                if (flag == false) {
                    return "添加失败";
                }
                return "添加成功";
            }
            return "仓库名或仓库地址已存在";
        }
        return "添加失败";
    }

    //获取数据表格的方法
    @RequestMapping("/getWarehouseList")
    @ResponseBody
    public Map<String, Object> getWarehouseList(@RequestBody PageInf pageInf) {
        System.out.println("当前页：" + pageInf.getPage() + ",几条数据为一页：" + pageInf.getSize());
        pageInf.setWarehouseStateId(1);
        RowBounds row = new RowBounds((pageInf.getPage() - 1) * pageInf.getSize(), pageInf.getSize());
        List<TbWarehouse> warehouseList = tbWarehouseService.getWarehouseList(pageInf, row);
        System.out.println(warehouseList.size());
        Map<String, Object> map = new HashMap<>();
        map.put("count", tbWarehouseService.count(pageInf));
        map.put("warehouseList", warehouseList);
        return map;

    }

    //删除数据的方法
    @RequestMapping("delWarehouse")
    @ResponseBody
    public String delWarehouse(int warehouseId) {
        System.out.println(warehouseId);
        boolean flag = tbWarehouseService.delWarehouse(warehouseId, 2);
        if (flag == true) {
            return "删除成功";
        }
        return "删除失败";
    }

    //修改仓库的信息
    @RequestMapping("updateWarehouse")
    @ResponseBody
    public String updateWarehouse(@RequestBody TbWarehouse tbWarehouse) {
        tbWarehouse.setModifyTime(GetTime.getTime());
        TbWarehouse tw = tbWarehouseService.judgeWarehouseName(tbWarehouse);
        if (tw == null) {
            tbWarehouseService.updateWarehouse(tbWarehouse);
            return "修改成功";
        }
        return "仓库名或地址重名请重新修改";
    }
}
